package com.framework.base.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.framework.base.constant.WebConstants;
import com.framework.base.struts.BaseVo;
import com.framework.main.sys.user.domain.UserVo;

public class Struts2Utils {
	private static Logger logger = Logger.getLogger(QueryUtil.class); // 作为日志的公用对象
	private Struts2Utils() {
	}
	
	public static Logger getLogger() {
		return logger;
	}
	// 取得Request/Response/Session的简化函数 //

	/**
	 * 取得HttpSession的简化方法.
	 */
	public static HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 取得HttpRequest的简化方法.
	 */
	public static HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 取得HttpResponse的简化方法.
	 */
	public static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 取得Request Parameter的简化方法.
	 */
	public static String getParameter(String name) {
		return getRequest().getParameter(name);
	}
	/**
	 * 获取当前用户所在部门id结果集
	 * @return
	 */
	public static String getCurrentUserDeptId(){
		return (String) getSession().getAttribute(WebConstants.CURRENTUSERDEPTID);
	}
	/**
	 * 获取当前用户部门名称结果集
	 * @return
	 */
	public static String getCurrentUserDeptName(){
		return (String) getSession().getAttribute(WebConstants.CURRENTUSERDEPTNAME);
	}
	
	/**
	 * 获取当前用户
	 * @return
	 */
	public static UserVo getCurrentUser(){
		return (UserVo) getSession().getAttribute(WebConstants.CURRENTUSER);
	}
	
	/**
	 * 用于获取参数map
	 * @return
	 */
	public static Map<String,Object> getParameterMap(HashMap<String,Object> paramsMap,BaseVo vo){
		if(paramsMap==null){
			paramsMap=new HashMap<String, Object>();
		}
		//1添加当前用户
		paramsMap.put(WebConstants.CURRENTUSER, getCurrentUser());
		//2添加分页相关参数 并判断当前页不存在的情况下添加
		if(paramsMap.get(WebConstants.CURRENTPAGE)==null){
			paramsMap.put(WebConstants.CURRENTPAGE,getRequest().getParameter(WebConstants.CURRENTPAGE));
			paramsMap.put(WebConstants.ORDERFIDS,getRequest().getParameter(WebConstants.ORDERFIDS));
			paramsMap.put(WebConstants.ORDERTYPE,getRequest().getParameter(WebConstants.ORDERTYPE));
			paramsMap.put(WebConstants.CHANGETYPE,getRequest().getParameter(WebConstants.CHANGETYPE));
		}
		//3添加vo的信息 用于生成sql
		if(vo!=null)paramsMap.put(WebConstants.CLASS,vo.getClass());
		return paramsMap;
	}
	/**
	 * 判断数组并返回参数值
	 * @param paramsName
	 * @param arr
	 * @param isToStr 是否将数据转为字符串
	 * @return
	 */
	public static Object getParamValue(Object valueObj,boolean isToStr){
		String[] strarr=(String[]) valueObj;
		Object value="";
		if(valueObj!=null){
			//1.对于非数组的处理 直接取第一个值
			if(strarr.length==1){
				value=strarr[0];
			}
			//2.对于数组的处理，如果作为查询参数，则以数组的形式返回
			//作为参数转发，则以,号隔开的字符串形式返回
			if(strarr.length>1&&isToStr){
				value=ArrayUtil.getStringFormArr(strarr); //将数组转为字符串,并以逗号进行隔开
			}
			if(strarr.length>1&&!isToStr){
				value=valueObj;
			}
		}
		return value;
	}
	/**
	 * 将字符串中的参数转化为map
	 * @param paramsUrl
	 * @param paramsMap
	 */

	public static HashMap<String,Object> initQueryMap(String paramsUrl,HashMap<String,Object> paramsMap){
		if(paramsMap==null){
			paramsMap=new HashMap<String, Object>();
		}else{
			for (Iterator iter = paramsMap.keySet().iterator(); iter.hasNext();) { 
			      Object key = iter.next(); 
			      Object val = paramsMap.get(key); 
			      if(val!=null){
				      if(val.toString().indexOf("\"\"") != -1 || val.toString().indexOf("\"") != -1){
				    	  String value = val.toString().replaceAll("\"\"", "").replaceAll("\"", "");
				    	  paramsMap.put(key.toString(), value);
				      }
			      }
			  } 
		}
		if(!StringUtil.isNullorEmpty(paramsUrl)){
			String[] params=paramsUrl.split(",");
			for (String param : params) {
				int splitIndex=param.lastIndexOf("=");
				if(splitIndex>=0){
					String paramName=param.substring(0,splitIndex);
					String paramValue=param.substring(splitIndex+1,param.length());
					logger.info(paramName+":"+paramValue);
					paramsMap.put(paramName, paramValue);
				}
			}
		}
		return paramsMap;
	}
	/**
	 * 用于转发参数
	 * @param paramsMap 需要转发的参数
	 */
	public static void forwardParams(Map<String, Object> paramsMap){
		if(paramsMap!=null){
			Set<String> keySet=paramsMap.keySet();
			for (String key : keySet) {
				getRequest().setAttribute(key, paramsMap.get(key));
			}
		}
	}
	/**
	 * 对request中的参数进行转化
	 * @return
	 */
	public static Map<String,Object> getRequestMap(Map<String,Object> requestMap){
		Set<String> keySet = requestMap.keySet();
		HashMap<String, Object> resultMap=new HashMap<String, Object>();
		for (String key : keySet) {
			Object obj=requestMap.get(key);
			resultMap.put(key,getParamValue(obj,false));
		}
		return resultMap;
	}
}
